package com.ruoyi.api.wsmessage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.api.wsmessage.mapper.WsmessageMapper;
import com.ruoyi.api.wsmessage.domain.Wsmessage;
import com.ruoyi.api.wsmessage.service.IWsmessageService;
import com.ruoyi.common.core.text.Convert;

/**
 * 万顺消息Service业务层处理
 *
 * @author xzshan
 * @date 2020-06-16
 */
@Service
public class WsmessageServiceImpl implements IWsmessageService {
  @Autowired
  private WsmessageMapper wsmessageMapper;

  /**
   * 查询万顺消息
   *
   * @param id 万顺消息ID
   * @return 万顺消息
   */
  @Override
  public Wsmessage selectWsmessageById(Long id) {
    return wsmessageMapper.selectWsmessageById(id);
  }

  /**
   * 查询万顺消息列表
   *
   * @param wsmessage 万顺消息
   * @return 万顺消息
   */
  @Override
  public List<Wsmessage> selectWsmessageList(Wsmessage wsmessage) {
    return wsmessageMapper.selectWsmessageList(wsmessage);
  }

  /**
   * 新增万顺消息
   *
   * @param wsmessage 万顺消息
   * @return 结果
   */
  @Override
  public int insertWsmessage(Wsmessage wsmessage) {
    wsmessage.setCreateTime(DateUtils.getNowDate());
    return wsmessageMapper.insertWsmessage(wsmessage);
  }

  /**
   * 修改万顺消息
   *
   * @param wsmessage 万顺消息
   * @return 结果
   */
  @Override
  public int updateWsmessage(Wsmessage wsmessage) {
    return wsmessageMapper.updateWsmessage(wsmessage);
  }

  /**
   * 删除万顺消息对象
   *
   * @param ids 需要删除的数据ID
   * @return 结果
   */
  @Override
  public int deleteWsmessageByIds(String ids) {
    return wsmessageMapper.deleteWsmessageByIds(Convert.toStrArray(ids));
  }

  public int updateWsmessageStateByIds(String ids) {
    return wsmessageMapper.updateWsmessageStateByIds(Convert.toStrArray(ids));
  }

  /**
   * 删除万顺消息信息
   *
   * @param id 万顺消息ID
   * @return 结果
   */
  @Override
  public int deleteWsmessageById(Long id) {
    return wsmessageMapper.deleteWsmessageById(id);
  }

  @Override
  public List<Wsmessage> listAllByType(Wsmessage wsmessage) {
    String[] types = Convert.toStrArray(wsmessage.getType());
    Map<String,Object> map=new HashMap<>();
    map.put("userReceiverLoginName",wsmessage.getUserReceiverLoginName());
    map.put("types",types);
    return wsmessageMapper.listAllByType(map);
  }

  @Override
  public void saveOrUpdateWsMsg(Wsmessage wsmessage) {
    Map<String, Object> map = new HashMap<>();
    map.put("instanceId", wsmessage.getInstanceId());
    map.put("userReceiverLoginName", wsmessage.getUserReceiverLoginName());
    List<Wsmessage> querylist = wsmessageMapper.querylist(map);
    if (querylist != null && querylist.size() > 0) {
     return;
    }
    //新增
    insertWsmessage(wsmessage);
  }

  @Override
  public void UpdateAllByinstanceId(Wsmessage wsmessage) {
    Map<String, Object> map = new HashMap<>();
    map.put("instanceId", wsmessage.getInstanceId());
    List<Wsmessage> querylist = wsmessageMapper.querylist(map);
    if (querylist != null && querylist.size() > 0) {
      for (Wsmessage tmp : querylist) {
        tmp.setTaskName(wsmessage.getTaskName());
        tmp.setUpdateTime(wsmessage.getUpdateTime());
        tmp.setState(0l);
        updateWsmessage(tmp);
      }
    }
  }

}
